package com.meiwenhui.sishuwujing;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.util.NodeList;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class CopyOfInjectContent extends Thread {

	private static final Logger LOG = Logger.getLogger(CopyOfInjectContent.class);

	Connection connection = null;

	public void run() {
		ComboPooledDataSource cpds = new ComboPooledDataSource();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = cpds.getConnection();
			preparedStatement = connection.prepareStatement("select count(*) as count from url where status =0");
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				int count = resultSet.getInt("count");
				System.out.println(count);
				int xh = (count / 20) + (count % 20 == 0 ? 0 : 1);
				int offset = 0, limit = 20;

				for (int i = 0; i < xh; i++) {
					offset = i * limit + 1;

					PreparedStatement _preparedStatement = null;
					ResultSet _resultSet = null;
					LOG.info(offset + "\t" + limit);
					_preparedStatement = connection.prepareStatement("SELECT * from url where status =0 order by id,book asc limit " + offset + "," + limit);

					_resultSet = _preparedStatement.executeQuery();
					while (_resultSet.next()) {
						String url = _resultSet.getString("url");
						getContent(url, _resultSet.getString("book"), _resultSet.getInt("id"));
					}
				}
			}
		} catch (SQLException e) {
			LOG.error(e.getLocalizedMessage(), e);
		} finally {
			try {
				preparedStatement.close();
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void getContent(String url, String book, Integer id) {
		PreparedStatement __preparedStatement = null;
		PreparedStatement ___preparedStatement = null;
		try {
			String regex = "([\u4e00-\u9fa5]+)";
			Matcher matcher = Pattern.compile(regex).matcher(url);
			while (matcher.find()) {
				url = url.replace(matcher.group(0), URLEncoder.encode(matcher.group(0), "utf-8"));
			}
			URLConnection urlConnection = new URL(url).openConnection();
			String content1 = IOUtils.toString(urlConnection.getInputStream(), "utf-8");

			Parser spiderParser = new Parser();

			spiderParser.setInputHTML(content1);

			AndFilter andFilter = new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("align", "left"));
			NodeList chapters = spiderParser.extractAllNodesThatMatch(andFilter);
			String content = chapters.toHtml();
			content = content.substring(content.indexOf(">") + 1, content.lastIndexOf("<"));
			__preparedStatement = connection.prepareStatement("insert into article values(null, ?, ?,?)");

			__preparedStatement.setString(1, book);
			__preparedStatement.setInt(2, id);
			__preparedStatement.setString(3, content);
			int effect1 = 0,effect = 0;
//			effect = __preparedStatement.executeUpdate();
//
//			if (effect > 0) {
//				___preparedStatement = connection.prepareStatement("update url set status =1 where id = ?");
//				___preparedStatement.setInt(1, id);
//				effect1 = ___preparedStatement.executeUpdate();
//			}
			LOG.info(Thread.currentThread().getId() + "\t" + id + "\t" + book + "\t" + url + "\t" + effect + "\t" + effect1);
		} catch (Exception e) {
			LOG.error("[" + id + "\t" + book + "\t" + url + "]" + e.getLocalizedMessage(), e);
		} finally {
			try {
				if (__preparedStatement != null)
					__preparedStatement.close();
				if (___preparedStatement != null)
					___preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		CopyOfInjectContent copyOfInjectContent = new CopyOfInjectContent();
		copyOfInjectContent.start();
		
	}

}
